package com.xuecheng.learning.service.impl;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.learning.config.PayNotifyConfig;
import com.xuecheng.learning.service.MyCourseTablesService;
import com.xuecheng.messagesdk.model.po.MqMessage;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: 接收消息通知
 * @date 2024/3/27 15:29
 */
@Service
public class ReceivePayNotifyService {
    
    @Resource
    MyCourseTablesService myCourseTablesService;
    
    @RabbitListener(queues = PayNotifyConfig.PAYNOTIFY_QUEUE)
    public void receive(Message message){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        byte[] body = message.getBody();
        String jsonString = new String(body);
        // 转成对象
        MqMessage mqMessage = JSON.parseObject(jsonString, MqMessage.class);
        // 解析消息的内容
        // chooseCourseid
        String chooseCourseId = mqMessage.getBusinessKey1();
        // 订单类型
        String orderType = mqMessage.getBusinessKey2();
        // 学习中心服务只要购买课程类的支付订单的结果
        if ("60201".equals(orderType)){
            // 根据消息内容，更新选课记录，向我的课程表插入记录
            boolean b = myCourseTablesService.saveChooseSuccess(chooseCourseId);
            if (!b){
                XueChengPlusException.cast("保存选课记录状态失败");
            }
        }

        

    }
    
}
